﻿/*
 *page插件，拼接url参数，重新发起查询请求
 */
; (function ($, window, document, undefined) {
    var Page = function (el, opt) {
        this.$e = el;
        this.defaults = {
            linkClass: "page-link",
        };
        this.options = $.extend({}, this.defaults, opt);
        this.url;
        this.orderBy = "orderBy";
        this.isAsc = "isAsc";
    }
    Page.prototype = {
        init: function () {
            this.url = new URL(document.location.href);
            if (this.url.searchParams.has("undefined")) {
                this.url.searchParams.delete("undefined");
            }
        },
        setUrlParam(name, value) {
            if (name === undefined || name == null || name == "") {
                console.warn("url参数名称格式不正确");
                return;
            }
            if (value === null || value === undefined || value === "") {
                if (this.url.searchParams.has(name)) {
                    this.url.searchParams.delete(name);
                }
                return;
            }
            this.url.searchParams.set(name, value);
        },
        //跳转
        goPage: function () {
            var $t = this;
            if (this.options.on) {
                this.options.on.forEach(function (e) {
                    if ($.isArray(e)) {
                        $t.setUrlParam(e[1], $(e[0]).val())
                    } else {
                        $t.setUrlParam($(e).attr("name"), $(e).val())
                    }
                })
            }
            location.href = this.url.href;
        },
        //排序
        onOrder: function (sel, name) {
            var $t = this;
            $(sel).addClass("sort");
            if (this.url.searchParams.get(this.orderBy) === name) {
                //之前有此字段排序操作
                console.log(this.url.searchParams.get(this.isAsc))
                if (this.url.searchParams.get(this.isAsc) === "false") {
                    console.log("isFalse")
                    $(sel).addClass("sort_desc")
                    //this.url.searchParams.set(this.isAsc, true);

                } else {
                    $(sel).addClass("sort_asc")
                    console.log("isTrue")
                    //this.url.searchParams.set(this.isAsc, false);
                }
            } else {
                //之前无排序操作
            }
            $(sel).on("click", function () {
                $t.url.searchParams.set($t.orderBy, name);
                if ($(this).hasClass("sort_asc")) {
                    $t.url.searchParams.set($t.isAsc, false);
                } else {
                    $t.url.searchParams.set($t.isAsc, true);
                }
                $t.goPage()
            })
        },
        addPage: function () {
            var $t = this;
            //翻页事件
            this.$e.find("." + this.options.linkClass).each(function (i, e) {
                $(e).on("click", function () {
                    $t.setUrlParam($(e).data("pg-n"), $(e).data("pg-i"));
                    $t.goPage();
                })
            })
            //查询按钮绑定刷新页面
            if (this.options.go) {
                //console.log(this.options.go);
                this.options.go.forEach(function (e) {
                    if ($.isArray(e)) {
                        //e:["#id","change"]
                        $(e[0]).on(e[1], function () {
                            $t.goPage();
                        })
                    } else {
                        //默认点击事件
                        $(e).on("click", function () {
                            $t.goPage();
                        })
                    }

                })
            }
            //排序
            if (this.options.order) {
                this.options.order.forEach(function (e) {
                    if ($.isArray(e)) {
                        //传入数组
                        $t.onOrder(e[0], e[1]);
                    } else {
                        //字符串
                        $t.onOrder(e, $(e).attr("name"));
                    }
                })
            }
            return this;
        }
    }
    $.fn.page = function (options) {
        var page = new Page(this, options);
        page.init();
        return page.addPage();
    };
})(jQuery, window, document);


//提示框Toast data类型：ToastResult
function Toast(data) {
    $(document).Toasts('create', { class: data.state, title: data.title, autohide: false, fixed: false, delay: 20000, body: data.body })
}